# -*- coding: utf-8 -*-
'''
Created on 20 Jul 2010

Shamelessly taken from J. P. Calderone's blog post at
http://jcalderone.livejournal.com/55680.html

@author: jafd
'''

from json import JSONEncoder

from twisted.internet.task import cooperate

class AsyncJSON(object):
    def __init__(self, value):
        self._value = value


    def beginProducing(self, consumer):
        self._consumer = consumer
        self._iterable = JSONEncoder().iterencode(self._value)
        self._consumer.registerProducer(self, True)
        self._task = cooperate(self._produce())
        d = self._task.whenDone()
        d.addBoth(self._unregister)
        return d


    def pauseProducing(self):
        self._task.pause()


    def resumeProducing(self):
        self._task.resume()


    def stopProducing(self):
        self._task.stop()


    def _produce(self):
        for chunk in self._iterable:
            self._consumer.write(chunk)
            yield None


    def _unregister(self, passthrough):
        self._consumer.unregisterProducer()
        return passthrough
